
<%@page import="java.awt.List"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="openvote.Candidato"%>
<%@page import="openvote.Eleitor"%>
<%@ page import="openvote.Votacao" %>
<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:set var="entityName" value="${message(code: 'votacao.label', default: 'Votacao')}" />
		<title><g:message code="default.list.label" args="[entityName]" /></title>
	</head>
	<body>
		<a href="#list-votacao" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
		<div class="nav" role="navigation">
			<ul>
				<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
				<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
			</ul>
		</div>
		<div id="list-votacao" class="content scaffold-list" role="main">
			<h1><g:message code="default.list.label" args="[entityName]" /></h1>
			<g:if test="${flash.message}">
			<div class="message" role="status">${flash.message}</div>
			</g:if>
			<table>
				<thead>
					<tr>
					
						<th><g:message code="votacao.candidato.label" default="Candidato" /></th>
						<th><g:message code="votacao.candidato.label" default="Numero" /></th>
						<th><g:message code="votacao.candidato.label" default="Partido" /></th>					
						<th><g:message code="votacao.total.label" default="Votos" /></th>						
						<th><g:message code="votacao.total.label" default="%" /></th>												
						
					</tr>
				</thead>
				<tbody>
															
				<g:each in="${Candidato.executeQuery("select distinct c.numero from Candidato c")}" status="i" var="votacaoInstance">
					<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">																											
										
									
					<td>${Candidato.executeQuery("select c.nome from Candidato c where c.numero=${votacaoInstance}").first()}
					<td>${Candidato.executeQuery("select c.numero from Candidato c where c.numero=${votacaoInstance}").first()}
					<td>${Candidato.executeQuery("select c.partido from Candidato c where c.numero=${votacaoInstance}").first()}
					<td>${Votacao.executeQuery("select count(*) from Votacao v where v.candidato.numero=${votacaoInstance}").first()}
					<td>${Votacao.count > 1 ?  Votacao.executeQuery("select count(*) from Votacao v where v.candidato.numero=${votacaoInstance}").first()/Votacao.count*100 : 0}	
					
			
				</g:each>								
				</tbody>												
			</table>		
	
			
			<h1>Vencedor: ${Votacao.count > 1 ? Votacao.executeQuery("select v.candidato.nome,count(*) from Votacao v group by v.candidato.nome order by 2 desc").first().toString() : null}</h1>						
			
			<h1>Total Geral de Votos: <b>${Votacao.count}</h1>
						
						
			<div class="pagination">
				<g:paginate total="${votacaoInstanceTotal}" />
			</div>
		</div>
	</body>
</html>
